package com.fengyi.homefurnish.controller;

import com.fengyi.homefurnish.pojo.GoodsBasicInfo;
import com.fengyi.homefurnish.service.GoodsBasicInfoService;
import com.fengyi.homefurnish.utils.Pager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.swing.filechooser.FileSystemView;
import java.io.File;
import java.io.IOException;
import java.util.Random;

@Controller
@RequestMapping("/goodsBasicInfo")
public class GoodsBasicInfoController {

    @Autowired
    private GoodsBasicInfoService goodsBasicInfoService;

    @ResponseBody
    @RequestMapping("/goodsBasicInfo.html")
    public Pager<GoodsBasicInfo> store(Integer offset,Integer limit){
        return goodsBasicInfoService.getGoodsSearch(offset,limit);
    }

    @ResponseBody
    @RequestMapping("/goodsBasicInfoupdate.html")
    public String goodsBasicInfoupdate(GoodsBasicInfo goodsBasicInfo){
        String info = "";
        try{
            goodsBasicInfoService.updateGoodsState(goodsBasicInfo);
            info = "success";
        }catch (Exception e){
            info = "error";
        }
        return info;
    }

    @RequestMapping("/goodsdetails.html")
    public String goodsdetails(Integer goodsBasicInfoId, Model model){
        model.addAttribute("goodsBasicInfo",goodsBasicInfoService.GetSearch(goodsBasicInfoId));
        model.addAttribute("proSeries",goodsBasicInfoService.proseries());
        model.addAttribute("proAttrName",goodsBasicInfoService.proAttrName());
        model.addAttribute("GoodsSearch",goodsBasicInfoService.GetGoodsPriceType());
        model.addAttribute("goodsPictureManager",goodsBasicInfoService.GetGoodsPictureManager(goodsBasicInfoId));
        return "goods/details/goodsdetails";
    }

    @ResponseBody
    @RequestMapping("/img")
    public String img(@RequestParam("file")MultipartFile attach, HttpServletRequest request){
        FileSystemView fileSys = FileSystemView.getFileSystemView();
        try {
            long t = System.currentTimeMillis();
            Random rd = new Random(t);
            String name = "FY"+rd.nextInt()+rd.nextInt(9999);
            File directory = new File("");
            String courseFile = directory.getCanonicalPath();
            String path = request.getContextPath();
            String prefix = attach.getOriginalFilename().substring(attach.getOriginalFilename().lastIndexOf(".")+1);
            String lujing = "/images/" + name + "." + prefix;
            String filePath = courseFile + "/src/main/resources/static/images/" + name + "." + prefix;
            File file = new File(filePath);
            fileSys.getSystemDisplayName(file);
            attach.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    @ResponseBody
    @RequestMapping("/addimg")
    public String addimg(@RequestParam("file")MultipartFile[] attach){
        System.out.println(attach.length);
        return "";
    }

}
